import os
from google.appengine.ext.webapp import template
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util

class MainHandler(webapp.RequestHandler):
  def get(self):  
    user = users.get_current_user()

    if user:
      user_nick = user.nickname()
      url = users.create_logout_url(self.request.uri)
      url_linktext = 'Logout'
      template_values = {
	'user_nick': user_nick,
	'url': url,
	'url_linktext': url_linktext,
	}
    else:
      url = users.create_login_url(self.request.uri)
      url_linktext = 'login'
      template_values = {
	'url': url,
	'url_linktext': url_linktext,
	}

    path = os.path.join(os.path.dirname(__file__), 'templates', 'main.html')
    self.response.out.write(template.render(path, template_values))

def main():
  application = webapp.WSGIApplication([
    ('/', MainHandler),
  ], debug=True)
  util.run_wsgi_app(application)

if __name__ == "__main__":
  main()
